clear all
cd "C:\Users\mdebus\Dropbox\soep"
use soep_maindataset.dta, clear

sort syear cid hid 
merge m:1 syear cid hid using region_sorted.dta
keep if _merge==3
drop _merge

sort syear pid 
merge 1:1 syear pid using ppathl_sorted.dta
keep if _merge==3


tab syear
tab plj0014_v3
gen deutsche=0
replace deutsche=1 if plj0014_v3==1

* position: worker

tab plb0058
tab plb0058, nol

tab plb0058 syear

gen arbeiter_broad = 0
replace arbeiter_broad = 1 if plb0058>0
tab arbeiter_broad

gen un_angelernte_arbeiter = 0
replace un_angelernte_arbeiter = 1 if plb0058==1 | plb0058==2 

gen gelernte_arbeiter = 0
replace gelernte_arbeiter = 1 if plb0058>2

* Position: self-employed, civil servants 
tab plb0065 if syear==2018

gen beamte_broad = 0
replace beamte_broad = 1 if plb0065>=1

gen selbstst_broad = 0
replace selbstst_broad=1 if plb0057_h1==1 | plb0057_h1==2 | plb0057_h1==3


* PI: existing 
tab plh0011_h
tab plh0012_h plh0011_h
* PI: harmonised 
tab plh0012_h
label list plh0012_h
replace plh0012_h = . if plh0012_h<-2
replace plh0012_h = . if plh0012_h==-1

* voting intention, BUndestag 2014 and 2018 (retrospective) 
tab plh0333 if syear==2018, mis
tab plh0333 if syear==2014, mis
drop if plh0333==-5 | plh0333==-4

* spd PI
gen spd=0
replace spd=1 if plh0012_h==1

* Union PI 
gen union=0
replace union=1 if plh0012_h==13 | plh0012_h==2 | plh0012_h==3

* Left PI 
gen linke = 0
replace linke = 1 if plh0012_h==6

* FDP PI 
gen fdp = 0
replace fdp = 1 if plh0012_h==4

* Greens PI 
gen gru = 0
replace gru = 1 if plh0012_h==5

* No PI 

gen keine_pid_ = 0
replace keine_pid = 1 if plh0011_h==2

* PI far right parties, AFD (7 [NPD, REP], 27 [AfD])

gen rechte_afd = 0
replace rechte_afd = 1 if plh0012_h==7 | plh0012_h==27


* Restructuring data 

tab syear, nol

gen y1984 = 0
replace y1984=1 if syear==1984
gen y1985 = 0
replace y1985=1 if syear==1985
gen y1986 = 0
replace y1986=1 if syear==1986
gen y1987 = 0
replace y1987=1 if syear==1987
gen y1988 = 0
replace y1988=1 if syear==1988
gen y1989 = 0
replace y1989=1 if syear==1989
gen y1990 = 0
replace y1990=1 if syear==1990
gen y1991 = 0
replace y1991=1 if syear==1991
gen y1992 = 0
replace y1992=1 if syear==1992
gen y1993 = 0
replace y1993=1 if syear==1993
gen y1994 = 0
replace y1994=1 if syear==1994
gen y1995 = 0
replace y1995=1 if syear==1995
gen y1996 = 0
replace y1996=1 if syear==1996
gen y1997 = 0
replace y1997=1 if syear==1997
gen y1998 = 0
replace y1998=1 if syear==1998
gen y1999 = 0
replace y1999=1 if syear==1999
gen y2000 = 0
replace y2000=1 if syear==2000
gen y2001 = 0
replace y2001=1 if syear==2001
gen y2002 = 0
replace y2002=1 if syear==2002
gen y2003 = 0
replace y2003=1 if syear==2003
gen y2004 = 0
replace y2004=1 if syear==2004
gen y2005 = 0
replace y2005=1 if syear==2005
gen y2006 = 0
replace y2006=1 if syear==2006
gen y2007 = 0
replace y2007=1 if syear==2007
gen y2008 = 0
replace y2008=1 if syear==2008
gen y2009 = 0
replace y2009=1 if syear==2009
gen y2010 = 0
replace y2010=1 if syear==2010
gen y2011 = 0
replace y2011=1 if syear==2011
gen y2012 = 0
replace y2012=1 if syear==2012
gen y2013 = 0
replace y2013=1 if syear==2013
gen y2014 = 0
replace y2014=1 if syear==2014
gen y2015 = 0
replace y2015=1 if syear==2015
gen y2016 = 0
replace y2016=1 if syear==2016
gen y2017 = 0
replace y2017=1 if syear==2017
gen y2018 = 0
replace y2018=1 if syear==2018
gen y2019 = 0
replace y2019=1 if syear==2019
gen y2020 = 0
replace y2020=1 if syear==2020

* sort by Person_ID and year

sort pid syear

bysort syear: tab spd arbeiter_broad, col row 

bysort syear: tab rechte_afd arbeiter_broad, col row

* labour union membership 
tab plh0263_h
tab plh0263_h if y2011==1

gen gewerk2011=0
replace gewerk2011=1 if y2011==1 & plh0263_h==1

bysort pid: egen labunion2011 = max(gewerk2011)

tab labunion2011 if y2014==1

gen gewerk2015=0
replace gewerk2015=1 if y2014==1 & plh0263_h==1
bysort pid: egen labunion2015 = max(gewerk2015)
tab labunion2015 if y2018==1

gen gewerk2019=0
replace gewerk2019=1 if y2019==1 & plh0263_h==1

bysort pid: egen labunion2019 = max(gewerk2019)

tab labunion2019 if y2018==1

* spd voters in 2014

gen spd_2014 = 0
replace spd_2014=1 if y2014==1 & plh0333==1
bysort pid: egen spd2014 = max(spd_2014)
tab spd2014 if y2018==1

tab spd2014 if syear==2018

tab plh0333 spd2014 if syear==2018, col

* CDU/CSU voters 2014
tab plh0333
gen union_2014 = 0
replace union_2014=1 if y2014==1 & (plh0333==2 | plh0333==3)
bysort pid: egen union2014 = max(union_2014)
tab union2014 if y2018==1

tab union2014 if syear==2018

tab plh0333 union2014 if syear==2018, col

* Green voters 2014
tab plh0333
gen gru_2014 = 0
replace gru_2014=1 if y2014==1 & (plh0333==5)
bysort pid: egen gru2014 = max(gru_2014)
tab gru2014 if y2018==1

tab gru2014 if syear==2018

tab plh0333 gru2014 if syear==2018, col

* FDP voters 2014

tab plh0333
gen fdp_2014 = 0
replace fdp_2014=1 if y2014==1 & (plh0333==4)
bysort pid: egen fdp2014 = max(fdp_2014)
tab fdp2014 if y2018==1

tab fdp2014 if syear==2018

tab plh0333 fdp2014 if syear==2018, col

* Left voters 2014

tab plh0333
gen lin_2014 = 0
replace lin_2014=1 if y2014==1 & (plh0333==6)
bysort pid: egen lin2014 = max(lin_2014)
tab lin2014 if y2018==1

tab lin2014 if syear==2018

tab plh0333 lin2014 if syear==2018, col

* Did note vote 2014  
tab plh0333
gen nw_2014 = 0
replace nw_2014=1 if y2014==1 & (plh0333==28)
bysort pid: egen nw2014 = max(nw_2014)
tab nw2014 if y2018==1

tab plh0333 nw2014 if syear==2018, col

*not allowed to vote 2014
tab plh0333
gen nwb_2014 = 0
replace nwb_2014=1 if y2014==1 & (plh0333==29)
bysort pid: egen nwb2014 = max(nwb_2014)
tab nwb2014 if y2018==1

tab plh0333 nwb2014 if syear==2018, col


* did a respondent ever had an SPD PI 

tab spd arbeiter_broad if y1984==1, col

sort pid syear
bysort pid: gen spd_t = spd[_n-1]

sort pid syear
bysort pid: gen union_t = union[_n-1]


* was präferieren die personen, die zum Zeitpunkt t-1 SPD präferiert haben, zum Zeitpnkt t aber nicht mehr?

* which parties prefer respondents, who preferred SPD at t-1, but not at time point t?

tab plh0012_h arbeiter if spd_t==1 & spd==0, col 
bysort syear: tab plh0012_h arbeiter if spd_t==1 & spd==0, col 

* Estimating "SPD remainers"

recode plh0012_h (3=2) (13=2)
bysort syear: tab plh0012_h arbeiter if spd_t==1 , col 

* which party prefer individuals who supported spd at t-2 and switched to the independents at t-1 at time point t? 

gen ohne_partei = 0
replace ohne_partei=1 if plh0012_h==-2

sort pid syear
bysort pid: gen spd_t2 = spd[_n-2] 

sort pid syear
bysort pid: gen ohne_partei_t1 = ohne_partei[_n-1] 
sort pid syear
bysort pid: gen ohne_partei_t2 = ohne_partei[_n-2]
sort pid syear
bysort pid: gen ohne_partei_t3 = ohne_partei[_n-3]  
sort pid syear
bysort pid: gen ohne_partei_t4 = ohne_partei[_n-4] 
sort pid syear
bysort pid: gen ohne_partei_t5 = ohne_partei[_n-5] 

bysort syear: tab plh0012_h arbeiter if spd_t2==1 & ohne_partei_t1==1, col 

* which party preferred individuals who switched from the SPD at t3 to "non affiliated" at t2 after three years? 

sort pid syear
bysort pid: gen spd_t3 = spd[_n-3] 

sort pid syear
bysort pid: gen union_t2 = union[_n-2] 
sort pid syear
bysort pid: gen union_t3 = union[_n-3] 
sort pid syear
bysort pid: gen union_t4 = union[_n-4] 
sort pid syear
bysort pid: gen union_t5 = union[_n-5] 

sort pid syear
bysort pid: gen linke_t = linke[_n-1] 
sort pid syear
bysort pid: gen linke_t2 = linke[_n-2] 
sort pid syear
bysort pid: gen linke_t3 = linke[_n-3] 
sort pid syear
bysort pid: gen linke_t4 = linke[_n-4] 
sort pid syear
bysort pid: gen linke_t5 = linke[_n-5] 

sort pid syear
bysort pid: gen fdp_t = fdp[_n-1] 
sort pid syear
bysort pid: gen fdp_t2 = fdp[_n-2] 
sort pid syear
bysort pid: gen fdp_t3 = fdp[_n-3] 
sort pid syear
bysort pid: gen fdp_t4 = fdp[_n-4] 
sort pid syear
bysort pid: gen fdp_t5 = fdp[_n-5] 

sort pid syear
bysort pid: gen gru_t = gru[_n-1] 
sort pid syear
bysort pid: gen gru_t2 = gru[_n-2] 
sort pid syear
bysort pid: gen gru_t3 = gru[_n-3] 
sort pid syear
bysort pid: gen gru_t4 = gru[_n-4] 
sort pid syear
bysort pid: gen gru_t5 = gru[_n-5] 



sort pid syear
bysort pid: gen spd_t4 = spd[_n-4] 

sort pid syear
bysort pid: gen spd_t5 = spd[_n-5] 

sort pid syear
bysort pid: gen spd_t6 = spd[_n-6] 

sort pid syear
bysort pid: gen spd_t7 = spd[_n-7] 

tab spd_t7

sort pid syear
bysort pid: gen spd_t8 = spd[_n-8]

sort pid syear
bysort pid: gen spd_t9 = spd[_n-9]

sort pid syear
bysort pid: gen spd_t10 = spd[_n-10] 
 
sort pid syear
bysort pid: gen spd_t11 = spd[_n-11] 

sort pid syear
bysort pid: gen spd_t12 = spd[_n-12] 

sort pid syear
bysort pid: gen spd_t13 = spd[_n-13] 

sort pid syear
bysort pid: gen spd_t14 = spd[_n-14] 

sort pid syear
bysort pid: gen spd_t15 = spd[_n-15] 


bysort syear: tab plh0012_h arbeiter if spd_t3==1 & ohne_partei_t2==1 & ohne_partei_t1==1, col 


* voting behaviour 2028

tab plh0333 if syear==2018
replace plh0333 = . if plh0333 == -1 | plh0333 == 29 | plh0333==-5

tab plh0333 spd_2014 if syear==2014, col

tab plh0333 spd_2014 if syear==2018, col



* What was the voting behavior in 2017 of respondents who identified the SPD as their "next party" one year before the survey (i.e. 2017), but no longer did so afterwards (i.e. 2018) and instead indicated no party affiliation?

bysort arbeiter_broad: tab plh0333 if spd_t==1 & ohne_partei==1 & syear==2018 & deutsche==1

* What was the voting behavior in 2017 of respondents who identified the SPD as their "next party" two years before the survey (i.e. 2016), but no longer identified it as their "next party" after the survey (i.e. 2017) and instead indicated no party affiliation?

tab plh0333 if spd_t2==1 & ohne_partei_t1==1 & syear==2018 & deutsche==1
bysort arbeiter_broad: tab plh0333 if spd_t2==1 & ohne_partei_t1==1 & syear==2018 & deutsche==1

* What was the voting behavior in 2017 of respondents who identified the SPD as their "next party" three years before the survey (i.e. 2015), but no longer identified it as their "next party" after the survey (i.e. 2016, 2017) and instead indicated no party affiliation?

tab plh0333 if spd_t3==1 & ohne_partei_t1==1 & ohne_partei_t2==1 & syear==2018 & deutsche==1
bysort arbeiter_broad: tab plh0333 if spd_t3==1 & ohne_partei_t1==1 & ohne_partei_t2==1 & syear==2018 & deutsche==1

*What was the voting behavior of respondents who identified the SPD as their "next party" 4 years before the survey (i.e. 2014), but no longer did so afterwards (i.e. 2015, 2016 and 2017) and instead indicated no party affiliation?

tab plh0333 if spd_t4==1 & ohne_partei_t1==1 & ohne_partei_t2==1 & ohne_partei_t3==1 & syear==2018 & deutsche==1

bysort arbeiter_broad: tab plh0333 if spd_t4==1 & ohne_partei_t1==1 & ohne_partei_t2==1 & ohne_partei_t3==1 & syear==2018 & deutsche==1


* retrospective voting 2014: And how was it at the last general election (Bundestagswahl) on September 22, 2013? Which party did you vote for? 
* what did the people who were still close to the spd in 2017 vote for in the 2017 BTW, but no longer in 2018?

bysort arbeiter_broad: tab plh0333 if spd_t==1 & spd==0 & syear==2018 & deutsche==1

* what did people who were still close to the spd in 2013 but no longer in 2014 vote for in the 2013 BTW?

bysort arbeiter_broad: tab plh0333 if spd_t==1 & spd==0 & syear==2014 & deutsche==1


* Identification of persons who once had an SPD ID and lost it in the following year 

sort pid syear 
gen spd_lost_1year = 0
replace spd_lost_1year = 1 if (spd_t==1) & spd==0

gen spd_lost_2year = 0
replace spd_lost_2year = 1 if (spd_t2==1) & spd==0

gen spd_lost_3year = 0
replace spd_lost_3year = 1 if (spd_t3==1) & spd==0

gen spd_lost_4year = 0
replace spd_lost_4year = 1 if (spd_t4==1) & spd==0

gen spd_lost_3years = 0
replace spd_lost_3years = spd_lost_1year+spd_lost_2year+spd_lost_3year

bysort pid: replace spd_lost_3years = 1 if (spd_t3==0 & spd_t2==0 & spd_t==0) & spd==1


* Keep survey years of interest (2014 and 2018)
keep if syear == 2018 | syear == 2014

* min. one pi in the 3 or 5 years before the survey and no current party id 

gen spd_last5y = 0
replace spd_last5y = 1 if (spd_t == 1 | spd_t2 == 1 | spd_t3 == 1 | spd_t4 == 1 | spd_t5 == 1) & ohne_partei==1

gen spd_last3y = 0
replace spd_last3y = 1 if (spd_t == 1 | spd_t2 == 1 | spd_t3 == 1) & ohne_partei==1

gen union_last3y = 0
replace union_last3y = 1 if (union_t == 1 | union_t2 == 1 | union_t3 == 1) & ohne_partei==1

gen union_last5y = 0
replace union_last5y = 1 if (union_t == 1 | union_t2 == 1 | union_t3 == 1  | union_t4 == 1  | union_t5 == 1) & ohne_partei==1

gen ohne_partei_last3y = 0
replace ohne_partei_last3y = 1 if (ohne_partei_t1 == 1 ///
| ohne_partei_t2 == 1 | ohne_partei_t3 == 1) & ohne_partei==1

gen ohne_partei_last5y = 0
replace ohne_partei_last5y = 1 if (ohne_partei_t1 == 1 | ohne_partei_t2 == 1 | ohne_partei_t3 == 1 | ohne_partei_t4 == 1  | ohne_partei_t5 == 1) & ohne_partei==1

gen linke_last3y = 0
replace linke_last3y = 1 if (linke_t == 1 ///
| linke_t2 == 1 | linke_t3 == 1) & ohne_partei==1

gen linke_last5y = 0
replace linke_last5y = 1 if (linke_t == 1 | linke_t2 == 1 | linke_t3 == 1 | linke_t4 == 1  | linke_t5 == 1) & ohne_partei==1


gen fdp_last3y = 0
replace fdp_last3y = 1 if (fdp_t == 1 ///
| fdp_t2 == 1 | fdp_t3 == 1) & ohne_partei==1

gen fdp_last5y = 0
replace fdp_last5y = 1 if (fdp_t == 1 | fdp_t2 == 1 | fdp_t3 == 1 | fdp_t4 == 1  | fdp_t5 == 1) & ohne_partei==1


gen gru_last3y = 0
replace gru_last3y = 1 if (gru_t == 1 ///
| gru_t2 == 1 | gru_t3 == 1) & ohne_partei==1

gen gru_last5y = 0
replace gru_last5y = 1 if (gru_t == 1 | gru_t2 == 1 | gru_t3 == 1 | gru_t4 == 1  | gru_t5 == 1) & ohne_partei==1

gen afd_2018 = 0
replace afd_2018 = 1 if plh0333==27 & syear==2018

tab plh0333 if syear==2014 & deutsche==1
gen afd_2014 = 0
replace afd_2014 = 1 if plh0333==27 & syear==2014



* PI AFD
tab plh0012_h

gen afd_pi = 0
replace afd_pi = 1 if plh0012_h==27

* Gender

gen female = 0
replace female = 1 if pla0009_v2==2
replace female = 1 if pla0009_v3==2

* Life satisfaction  
tab plh0182 if syear==2018
recode plh0182 -1=.
rename plh0182 zufriedenheit

* general trust 
tab plh0192 if syear==2018
recode  plh0192 -1=.
rename plh0192 vertrauen

tab vertrauen

* age

gen alter = syear-ple0010_h
tab alter
tab alter if syear==2018
tab alter if syear==2014

* age squared
gen alter_sq = alter^2

* university degree

gen hochschule = 0
replace hochschule=1 if plg0014_v7>1 

* currently not occupied

gen nicht_erwerbstaetig = 0
replace nicht_erwerbstaetig = 1 if plb0022_h ==9

* interest in politics

recode plh0007 -1=. 
rename plh0007 interesse_politik 
tab interesse_politik if syear==2018, mis 

* region east-west

gen east = 0
replace east = 1 if bula>10

tab birthregion

tab birthregion, nol

gen born_east = 0
replace born_east=1 if birthregion>11

* migrant background

tab migback
tab migback, nol

gen migrant=0
replace migrant=1 if migback==2 | migback==3

gen arbeiter_mighin = 0
replace arbeiter_mighin = 1 if arbeiter_broad==1 & migrant==1

gen arbeiter_omighin = 0
replace arbeiter_omighin = 1 if arbeiter_broad==1 & migrant==0

gen nomigrant = 1
replace nomigrant=0 if migrant==0

tab migrant germborn

* identification of country of birth
* Russland - 32, Kasachstan 74, Ukraine 78, Kirgistan 77, Tadschikistan 82 , Usbekistan 97, Weissrussland 132, Armenien 148, Turkmenistan 155, Bessarabien - 180
tab corigin
tab corigin, mis
label list corigin
gen born_ussr = 0
replace born_ussr = 1 if corigin==32 | corigin==78 | corigin==74 | corigin==77 | corigin==82 | corigin==97 | corigin==132 | corigin==148 | corigin==55 | corigin==180

tab born_ussr nomigrant, col

gen born_muslimworld_africa = 0
replace born_muslimworld_africa = 1 if corigin==2 | corigin==24 | corigin==25 | corigin==30 | corigin==33 | corigin==37  | corigin==43  | corigin==46  | corigin==47  | corigin==49 | corigin==50  | corigin==52  | corigin==53  | corigin==54  | corigin==57  | corigin==60  | corigin==65  | corigin==67  | corigin==79 | corigin==80  | corigin==81  | corigin==84  | corigin==85  | corigin==86  | corigin==87  | corigin==89  | corigin==90  | corigin==94  | corigin==95  | corigin==102  | corigin==110 | corigin==105 | corigin==111 | corigin==113  | corigin==120  | corigin==121  | corigin==126  | corigin==127  | corigin==128  | corigin==130 | corigin==135  | corigin==136 | corigin==138  | corigin==139 | corigin==140 | corigin==142  | corigin==143  | corigin==144 | corigin==147  | corigin==149 | corigin==150  | corigin==151 | corigin==152 | corigin==156 | corigin==158 | corigin==161  | corigin==162 | corigin==163 | corigin==166  | corigin==173 | corigin==174  | corigin==176 | corigin==178  | corigin==179 | corigin==183  | corigin==190 | corigin==193  | corigin==196

gen born_east_ussr = 0
replace born_east_ussr = 1 if born_east==1 
replace born_east_ussr = 1 if born_ussr==1 

gen born_foreign_except_ussr = 0
replace born_foreign_except_ussr = 1 if born_ussr~=1 & corigin~=1


*duplicates tag pid if syear==2018, generate(test_2018_)

*duplicates tag pid if syear==2014, generate(test_2014_)

label var afd_pi "AfD ID"
label var spd_last3y "SPD ID over the last 3 years" 
label var spd_last5y "SPD ID over the last 5 years" 
label var arbeiter_mighin "Blue-collar worker (migrant background)" 
label var arbeiter_broad "Blue-collar worker" 
label var arbeiter_omighin "Blue-collar worker (w/o migrant background)" 
label var union_last3y "CDU/CSU ID over the last 3 years"
label var union_last5y "CDU/CSU ID over the last 5 years"
label var ohne_partei_last3y "No party ID over the last 3 years"
label var ohne_partei_last5y "No party ID over the last 5 years"
label var linke_last3y "The Left ID over the last 3 years" 
label var linke_last5y "The Left ID over the last 5 years" 
label var gru_last3y "Green party ID over the last 3 years"
label var gru_last5y "Green party ID over the last 5 years"
label var fdp_last3y "FDP ID over the last 3 years" 
label var fdp_last5y "FDP ID over the last 5 years" 
label var beamte_broad "Civil servants" 
label var selbstst_broad "Self employed" 
label var female "Female" 
label var alter "Age" 
label var alter_sq "Age (squared)" 
label var hochschule "University degree" 
label var nicht_erwerbstaetig "without employment"  
label var vertrauen "General Trust high-low" 
label var zufriedenheit "Life satisfaction low-high" 
label var interesse_politik "Interest in politics strong-weak"
label var east "East Germany" 
label var migrant "Migrant background"
label var labunion2015 "Labour union membership (2015)"
label var labunion2011 "Labour union membership (2011)"
label var union2014 "Voted CDU/CSU in 2013"
label var fdp2014 "Voted FDP in 2013"
label var lin2014 "Voted Left in 2013"
label var gru2014 "Voted Green in 2013"
label var spd2014 "Voted SPD in 2013"
label var nw2014 "Did not vote in 2013"
label var nwb2014 "Not allowed to vote in 2013"
label var born_east "Born in East Germany"
label var born_ussr "Born in the former Soviet Union"
label var born_muslimworld_africa "Born in Africa or in the Muslim world"


exit
* descriptives

* (1) Entwicklung über Zeit?

tab plh0333 spd_last3y if syear==2018, col 
bysort arbeiter_broad: tab plh0333 spd_last3y if syear==2018, col 

tab plh0333 spd_last3y if syear==2014, col 
bysort arbeiter_broad: tab plh0333 spd_last3y if syear==2014, col 

tab plh0333 spd_last5y if syear==2018, col 
bysort arbeiter_broad: tab plh0333 spd_last5y if syear==2018, col 

tab plh0333 spd_last5y if syear==2014, col 
bysort arbeiter_broad: tab plh0333 spd_last5y if syear==2014, col 


label var afd_pi "AfD ID"
label var spd_last3y "SPD ID over the last 3 years" 
label var spd_last5y "SPD ID over the last 5 years" 
label var arbeiter_mighin "Blue-collar worker (migrant background)" 
label var arbeiter_broad "Blue-collar worker" 
label var arbeiter_omighin "Blue-collar worker (w/o migrant background)" 
label var union_last3y "CDU/CSU ID over the last 3 years"
label var union_last5y "CDU/CSU ID over the last 5 years"
label var ohne_partei_last3y "No party ID over the last 3 years"
label var ohne_partei_last5y "No party ID over the last 5 years"
label var linke_last3y "The Left ID over the last 3 years" 
label var linke_last5y "The Left ID over the last 5 years" 
label var gru_last3y "Green party ID over the last 3 years"
label var gru_last5y "Green party ID over the last 5 years"
label var fdp_last3y "FDP ID over the last 3 years" 
label var fdp_last5y "FDP ID over the last 5 years" 
label var beamte_broad "Civil servants" 
label var selbstst_broad "Self employed" 
label var female "Female" 
label var alter "Age" 
label var alter_sq "Age (squared)" 
label var hochschule "University degree" 
label var nicht_erwerbstaetig "without employment"  
label var vertrauen "General Trust high-low" 
label var zufriedenheit "Life satisfaction low-high" 
label var interesse_politik "Interest in politics strong-weak"
label var east "East Germany" 
label var migrant "Migrant background"
label var labunion2015 "Labour union membership (2015)"
label var labunion2011 "Labour union membership (2011)"
label var union2014 "Voted CDU/CSU in 2013"
label var fdp2014 "Voted FDP in 2013"
label var lin2014 "Voted Left in 2013"
label var gru2014 "Voted Green in 2013"
label var spd2014 "Voted SPD in 2013"
label var nw2014 "Did not vote in 2013"
label var nwb2014 "Not allowed to vote in 2013"
label var born_east "Born in East Germany"
label var born_ussr "Born in the former Soviet Union"
label var born_muslimworld_africa "Born in Africa or in the Muslim world"

* analyse

tab syear

********2018
**********
* Model 1
eststo clear

eststo: logit afd_2018 afd_pi ///
i.spd2014 i.arbeiter_broad /// 
union2014 ///
lin2014 ///
fdp2014 ///
gru2014 ///
nw2014 ///
nwb2014 ///
beamte_broad selbstst_broad ///
female alter hochschule nicht_erwerbstaetig /// 
vertrauen  ///
born_east born_ussr born_muslimworld_africa ///
if deutsche==1 & syear==2018

estimates store model1
coefplot model1  , drop(_cons) xline(0) level(95)


* Model 2
eststo: logit afd_2018 afd_pi ///
i.spd2014##i.arbeiter_broad /// 
union2014 ///
lin2014 ///
fdp2014 ///
gru2014 ///
nw2014 ///
nwb2014 ///
beamte_broad selbstst_broad ///
female alter hochschule nicht_erwerbstaetig /// 
vertrauen  ///
born_east born_ussr born_muslimworld_africa ///
if deutsche==1 & syear==2018

estimates store model2
coefplot model2  , drop(_cons) xline(0) level(95)

margins spd2014, over(arbeiter_broad)
marginsplot ///
	, xdimension(arbeiter_broad) ///
		plotopts(connect(none)) ///
		title("") ///
		xlabel(-1 " " 0 "No blue-collar worker" 1 "Blue-collar worker" 2 " ", notick) ///
		xsize(4) ///
		xtitle(" ") ///
		ylabel(,angle(0)) ///
		ytitle("") ///
		plotregion(style(none) istyle(none)) ///
		plot1opts(lcolor(black) lwidth(thick)) ///
		level(90) ///
		legend(order(1 "Did not vote SPD in 2013" 2 "Voted SPD in 2013") position(6) region(style(none)))
		

* Model 3

eststo: logit afd_2018 afd_pi ///
i.spd_last3y i.arbeiter_broad /// 
union_last3y ///
linke_last3y ///
fdp_last3y ///
gru_last3y ///
ohne_partei_last3y ///
beamte_broad selbstst_broad ///
female alter hochschule nicht_erwerbstaetig /// 
vertrauen  ///
born_east born_ussr born_muslimworld_africa ///
if deutsche==1 & syear==2018

estimates store model3
coefplot model3  , drop(_cons) xline(0) level(95)

* Model 4 
eststo: logit afd_2018 afd_pi ///
i.spd_last3y##i.arbeiter_broad /// 
union_last3y ///
ohne_partei_last3y ///
linke_last3y ///
fdp_last3y ///
gru_last3y ///
beamte_broad selbstst_broad ///
female alter hochschule nicht_erwerbstaetig /// 
vertrauen  ///
born_east born_ussr born_muslimworld_africa ///
if deutsche==1 & syear==2018

estimates store model4
coefplot model4  , drop(_cons) xline(0) level(95)


margins spd_last3y, over(arbeiter_broad)
marginsplot ///
	, xdimension(arbeiter_broad) ///
		plotopts(connect(none)) ///
		title("") ///
		xlabel(-1 " " 0 "No blue-collar worker" 1 "Blue-collar worker" 2 " ", notick) ///
		xsize(4) ///
		xtitle(" ") ///
		ylabel(,angle(0)) ///
		ytitle("") ///
		plotregion(style(none) istyle(none)) ///
		plot1opts(lcolor(black) lwidth(thick)) ///
		level(90) ///
		legend(order(1 "Not close to SPD in last 3 years" 2 "Close to SPD in last 3 years") position(6) region(style(none)))

tab arbeiter_mighin migrant

		
esttab using "C:\Users\mdebus\Dropbox\soep\models_1-4_24Sept2024.rtf" , replace ///
	abs b(3) se(3) ///
	star(+ 0.10 * 0.05 ** 0.01) ///
	aic ///
	scalars("ll Log likelihood") ///
	nonumbers /// 
	coeflabels(_cons "Constant" ///
	afd_pi "AfD ID" ///
spd_last3y "SPD ID over the last 3 years" ///
arbeiter_mighin "Blue-collar worker (migrant background)" ///
arbeiter_broad "Blue-collar worker" ///
arbeiter_omighin "Blue-collar worker (w/o migrant background)" ///
union_last3y "CDU/CSU ID over the last 3 years" ///
ohne_partei_last3y "No party ID over the last 3 years" ///
linke_last3y "The Left ID over the last 3 years" ///
gru_last3y "Green party ID over the last 3 years" ///
fdp_last3y "FDP ID over the last 3 years" ///
beamte_broad "Civil servants" ///
selbstst_broad "Self employed" ///
female "Female" ///
alter "Age" ///
alter_sq "Age (squared)" ///
hochschule "University degree" ///
nicht_erwerbstaetig "without employment" /// 
vertrauen "General Trust high-low" ///
zufriedenheit "Life satisfaction low-high" ///
interesse_politik "Interest in politics strong-weak" ///
east "East Germany" ///
migrant "Migrant background") ///
	nonotes addnotes("Note: The dependent variable is the reported AfD vote choice in the 2017 Bundestag election. Numbers in parentheses are standard errors. Significance levels: + = p > 0.1; * > p = 0.05; ** = p > 0.01.") 
			

* 5 year interval
* Table A1
* Model 1

eststo clear
eststo: logit afd_2018 afd_pi ///
spd_last5y arbeiter_broad /// 
union_last5y ///
linke_last5y ///
gru_last5y ///
fdp_last5y ///
ohne_partei_last5y ///
beamte_broad selbstst_broad ///
female alter hochschule nicht_erwerbstaetig /// 
vertrauen ///
born_east born_ussr born_muslimworld_africa ///
if deutsche==1 & syear==2018

estimates store model1_2018_5y

coefplot model1_2018_5y  , drop(_cons) xline(0) level(95)


* Model 2

eststo: logit afd_2018 afd_pi ///
i.spd_last5y##i.arbeiter_broad /// 
union_last5y ///
ohne_partei_last5y ///
linke_last5y ///
gru_last5y ///
fdp_last5y ///
beamte_broad selbstst_broad ///
female alter hochschule nicht_erwerbstaetig /// 
vertrauen ///
born_east born_ussr born_muslimworld_africa ///
if deutsche==1 & syear==2018
	
estimates store model2_2018_5y

coefplot model2_2018_5y  , drop(_cons) xline(0) level(95)

margins spd_last5y, over(arbeiter_broad)
*margins arbeiter_broad, over(spd_last3y)

marginsplot ///
	, xdimension(arbeiter_broad) ///
		plotopts(connect(none)) ///
		title("") ///
		xlabel(-1 " " 0 "No blue-collar worker" 1 "Blue-collar worker" 2 " ", notick) ///
		xsize(4) ///
		xtitle(" ") ///
		ylabel(,angle(0)) ///
		ytitle("") ///
		plotregion(style(none) istyle(none)) ///
		plot1opts(lcolor(black) lwidth(thick)) ///
		level(90) ///
		legend(order(1 "Not close to SPD in last 5 years" 2 "Close to SPD in last 5 years") position(6) region(style(none)))

		
esttab using "C:\Users\mdebus\Dropbox\soep\models_5year_24Sept2024.rtf" , replace ///
	abs b(3) se(3) ///
	star(+ 0.10 * 0.05 ** 0.01) ///
	aic ///
	scalars("ll Log likelihood") ///
	nonumbers /// 
	coeflabels(_cons "Constant" ///
	afd_pi "AfD ID" ///
spd_last5y "SPD ID over the last 5 years" ///
arbeiter_mighin "Blue-collar worker (migrant background)" ///
arbeiter_broad "Blue-collar worker" ///
arbeiter_omighin "Blue-collar worker (w/o migrant background)" ///
union_last5y "CDU/CSU ID over the last 5 years" ///
fdp_last5y "FDP ID over the last 5 years" ///
ohne_partei_last5y "No party ID over the last 5 years" ///
linke_last5y "The Left ID over the last 5 years" ///
gru_last5y "Green ID over the last 5 years" ///
beamte_broad "Civil servants" ///
selbstst_broad "Self employed" ///
female "Female" ///
alter "Age" ///
alter_sq "Age (squared)" ///
hochschule "University degree" ///
nicht_erwerbstaetig "without employment" /// 
vertrauen "General Trust high-low" ///
zufriedenheit "Life satisfaction low-high" ///
interesse_politik "Interest in politics strong-weak" ///
east "East Germany" ///
migrant "Migrant background") ///
	nonotes addnotes("Note: The dependent variable the bargaining duration (in days). Numbers in parentheses are standard errors. Significance levels: + = p > 0.1; * > p = 0.05; ** = p > 0.01.") 
		
		
**************
*2014*********
* Model 1 
**************
tab vertrauen if deutsche==1 & syear==2014

eststo clear
eststo: logit afd_2014 afd_pi ///
spd_last3y arbeiter_broad ///
union_last3y ///
linke_last3y ///
fdp_last3y ///
gru_last3y ///
ohne_partei_last3y ///
beamte_broad selbstst_broad ///
female alter hochschule nicht_erwerbstaetig /// 
vertrauen  ///
born_muslimworld_africa born_east born_ussr ///
if deutsche==1 & syear==2014

estimates store model1_2014_3y
coefplot model1_2014_3y  , drop(_cons) xline(0) level(95)

* model 2

eststo: logit afd_2014 afd_pi ///
i.spd_last3y##i.arbeiter_broad ///
union_last3y ///
linke_last3y ///
fdp_last3y ///
gru_last3y ///
ohne_partei_last3y ///
beamte_broad selbstst_broad ///
female alter  hochschule nicht_erwerbstaetig /// 
vertrauen ///
born_east born_ussr born_muslimworld_africa ///
if deutsche==1 & syear==2014

estimates store model2_2014_3y

coefplot model2_2014_3y  , drop(_cons) xline(0) level(95)

margins spd_last3y, over(arbeiter_broad)
*margins arbeiter_broad, over(spd_last3y)

marginsplot ///
	, xdimension(arbeiter_broad) ///
		plotopts(connect(none)) ///
		title("") ///
		xlabel(-1 " " 0 "No blue-collar worker" 1 "Blue-collar worker" 2 " ", notick) ///
		xsize(4) ///
		xtitle(" ") ///
		ylabel(,angle(0)) ///
		ytitle("") ///
		plotregion(style(none) istyle(none)) ///
		plot1opts(lcolor(black) lwidth(thick)) ///
		level(90) ///
		legend(order(1 "Not close to SPD in last 3 years" 2 "Close to SPD in last 3 years") position(6) region(style(none)))


* Table A2
* 5 years period

* Model 1
eststo: logit afd_2014 afd_pi ///
spd_last5y arbeiter_broad ///
union_last5y ///
linke_last5y ///
fdp_last5y ///
gru_last5y ///
ohne_partei_last5y ///
beamte_broad selbstst_broad ///
female alter  hochschule nicht_erwerbstaetig /// 
vertrauen  ///
born_east born_ussr born_muslimworld_africa ///
if deutsche==1 & syear==2014

estimates store model1_2014_5y
coefplot model1_2014_5y  , drop(_cons) xline(0) level(95)

* model 2

eststo: logit afd_2014 afd_pi ///
i.spd_last5y##i.arbeiter_broad ///
union_last5y ///
linke_last5y ///
fdp_last5y ///
gru_last5y ///
ohne_partei_last5y ///
beamte_broad selbstst_broad ///
female alter  hochschule nicht_erwerbstaetig /// 
vertrauen ///
born_east born_ussr born_muslimworld_africa ///
if deutsche==1 & syear==2014

estimates store model2_2014_5y

coefplot model2_2014_5y  , drop(_cons) xline(0) level(95)

margins spd_last5y, over(arbeiter_broad)
marginsplot ///
	, xdimension(arbeiter_broad) ///
		plotopts(connect(none)) ///
		title("") ///
		xlabel(-1 " " 0 "No blue-collar worker" 1 "Blue-collar worker" 2 " ", notick) ///
		xsize(4) ///
		xtitle(" ") ///
		ylabel(,angle(0)) ///
		ytitle("") ///
		plotregion(style(none) istyle(none)) ///
		plot1opts(lcolor(black) lwidth(thick)) ///
		level(90) ///
		legend(order(1 "Not close to SPD in last 5 years" 2 "Close to SPD in last 5 years") position(6) region(style(none)))

esttab using "C:\Users\mdebus\Dropbox\soep\models_3and5year_2014_24Sept2024.rtf" , replace ///
	abs b(3) se(3) ///
	star(+ 0.10 * 0.05 ** 0.01) ///
	aic ///
	scalars("ll Log likelihood") ///
	nonumbers /// 
	coeflabels(_cons "Constant" ///
	afd_pi "AfD ID" ///
spd_last5y "SPD ID over the last 5 years" ///
arbeiter_mighin "Blue-collar worker (migrant background)" ///
arbeiter_broad "Blue-collar worker" ///
arbeiter_omighin "Blue-collar worker (w/o migrant background)" ///
union_last5y "CDU/CSU ID over the last 5 years" ///
ohne_partei_last5y "No party ID over the last 5 years" ///
linke_last5y "The Left ID over the last 5 years" ///
fdp_last5y "FDP ID over the last 5 years" ///
gru_last5y "Green party ID over the last 5 years" ///
beamte_broad "Civil servants" ///
selbstst_broad "Self employed" ///
female "Female" ///
alter "Age" ///
alter_sq "Age (squared)" ///
hochschule "University degree" ///
nicht_erwerbstaetig "without employment" /// 
vertrauen "General Trust high-low" ///
zufriedenheit "Life satisfaction low-high" ///
interesse_politik "Interest in politics strong-weak" ///
east "East Germany" ///
migrant "Migrant background") ///
	nonotes addnotes("Note: The dependent variable the bargaining duration (in days). Numbers in parentheses are standard errors. Significance levels: + = p > 0.1; * > p = 0.05; ** = p > 0.01.") 

